/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.test;

import com.comac.ins.isfd.service.IIsfdMeshOP2ParseService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bson.Document;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


@SpringBootTest
public class IsfdMeshOP2GenreateEntityListTest {
    @Autowired
    private IIsfdMeshOP2ParseService isfdMeshOP2ParseService;



    @BeforeAll
    static void setup() {
        System.out.println("初始化测试环境...");
    }

    @Test
    @Order(1)
    public void generateEntityList() {
        String UPLOAD_ID = "67ee38d0445e661dc1b0b781";

        List<String> resultNames = new ArrayList<>();
        resultNames.add("force.cbar_force");
        resultNames.add("force.crod_force");
        resultNames.add("force.cquad4_force");
        resultNames.add("force.ctria3_force");
        resultNames.add("mpc_forces");
        resultNames.add("spc_forces");

        Map<String, List<Integer>> resultNameIdMap = new HashMap<>();

        for (String resultName : resultNames) {
            List<Document> records = isfdMeshOP2ParseService.getAllRecords(UPLOAD_ID, resultName,21201, 1, 1000 );
            List<Integer> entityIds = records.stream()
                .map(record -> record.getInteger("entity"))  // 提取 entityId 字段
                .filter(Objects::nonNull)  // 过滤掉 null 的值（如果需要）
                .toList();  // 收集到 List
            resultNameIdMap.put(resultName, entityIds);
        }

        // 创建 Excel 文件
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Entity List");

        // 设置表头
        Row headerRow = sheet.createRow(0);
        int columnIndex = 0;
        for (String resultName : resultNames) {
            Cell cell = headerRow.createCell(columnIndex++);
            cell.setCellValue(resultName);
        }

        // 填充数据
        int maxRows = resultNameIdMap.values().stream()
            .mapToInt(List::size)
            .max()
            .orElse(0);  // 获取最大的 entityId 列表长度

        for (int rowIndex = 1; rowIndex <= maxRows; rowIndex++) {
            Row row = sheet.createRow(rowIndex);
            columnIndex = 0;
            for (String resultName : resultNames) {
                List<Integer> entityIds = resultNameIdMap.get(resultName);
                Cell cell = row.createCell(columnIndex++);
                if (rowIndex - 1 < entityIds.size()) {
                    cell.setCellValue(entityIds.get(rowIndex - 1));
                }
            }
        }

        // 保存 Excel 文件
        try (FileOutputStream fileOut = new FileOutputStream("EntityList.xlsx")) {
            workbook.write(fileOut);
            System.out.println("Excel 文件已成功生成！");
        } catch (IOException e) {
            System.err.println("生成 Excel 文件失败: " + e.getMessage());
        }


        System.out.println("success");
    }

    @AfterAll
    static void cleanup() {
        System.out.println("清理测试环境...");
    }
}
